package com.linkgie.galaxyframework.utils.concurrent;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

public class NamedThreadFactory implements ThreadFactory {

	private AtomicLong count = new AtomicLong(0);
	private String nameFormat;

	public NamedThreadFactory() {
	}

	public NamedThreadFactory(String nameFormat) {
		this.nameFormat = nameFormat;
	}

	@Override
	public Thread newThread(Runnable r) {
		return new Thread(r, formatThreadName(count.getAndIncrement()));
	}

	protected String formatThreadName(long id) {
		return nameFormat == null ? String.format("NamedThread-%d", id) : String.format(nameFormat, id);
	}

}
